package com.zj.sy.config;

import com.bjbz.common.threadlocal.ThreadlocalContext;
import com.zj.sy.config.cache.CacheConfig;
import com.zj.sy.config.cache.CacheManager;
import com.zj.sy.config.properties.BaseProperties;
import com.zj.sy.config.redis.RedisService;
import com.zj.sy.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Component
public class LoginFilter extends OncePerRequestFilter {

    public final static String HEADER = "Authorization";
    @Autowired
    private BaseProperties baseProperties;


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        String urlPath = request.getServletPath();

        //非授权请求过滤
        PathMatcher matcher = new AntPathMatcher();
        List<String> noAuthPathList = baseProperties.getNoAuthPath();
        if (noAuthPathList.parallelStream().anyMatch(p -> matcher.match(p, urlPath))) {
            chain.doFilter(request, response);
            return;
        }

        //需授权请求
//        String authToken = request.getHeader(HEADER);
//        if (authToken == null) {
//            authToken = request.getParameter(HEADER);
//        }
//
//        if (authToken == null || authToken.equals("") || authToken.toLowerCase().equals("null")) {
//            ResponseUtil.invalidToken(response);
//            return;
//        } else {
//            ThreadlocalContext threadlocalContext = CacheManager.getShallow(CacheConfig.CacheTypeEnum.shared_web_token, authToken, ThreadlocalContext.class);
//            if (threadlocalContext == null || threadlocalContext.getIfForbidden().equals(1)) {
//                ResponseUtil.invalidToken(response);
//                return;
//            }
//
//        }
        chain.doFilter(request, response);
    }


}
